﻿using System.Collections.Generic;
using System.Collections;
using System;
using UnityEngine;
using LitJson;

//管理ui的框架
public class UIManager : MonoBehaviour
{
    public static UIManager Instance
    {
        get
        {
            if (_Instance == null)
            {
                _Instance = FindObjectOfType<UIManager>();
            }
            if (!_Instance)
            {
                GameObject obj = new GameObject("UIManager");
                _Instance = obj.AddComponent<UIManager>();
            }
            return _Instance;
        }
        set
        {
            _Instance = value;
        }
    }
    private static UIManager _Instance;

    private Transform canvasTransform;
    private Transform CanvasTransform
    {
        get
        {
            if (canvasTransform == null)
            {
                canvasTransform = GameObject.Find("Canvas").transform;
            }
            return canvasTransform;
        }
    }

    private Dictionary<string, string> panelPathDict;//记录页面与路径对应关系
    private Dictionary<string, BasePanel> panelDict;//记录页面与名字对应关系的字典
    public Stack<BasePanel> panelStack;//ui页面栈

    

    private void Awake()
    {
        ParseUIPanelTypeJson();
    }

    public void PushPanel(string panelType)
    {
        if (panelStack == null)
        {
            panelStack = new Stack<BasePanel>();
        }

        //停止上一个界面
        if (panelStack.Count > 0)
        {
            BasePanel topPanel = panelStack.Peek();
            topPanel.OnPause();
        }
        //Debug.Log("push "+panelType);
        BasePanel panel = GetPanel(panelType);
        panelStack.Push(panel);
        panel.OnEnter();
    }

    public void PushPanel(string panelType,Vector3 ScreenPos)
    {
        if (panelStack == null)
        {
            panelStack = new Stack<BasePanel>();
        }

        //停止上一个界面
        if (panelStack.Count > 0)
        {
            BasePanel topPanel = panelStack.Peek();
            topPanel.OnPause();
        }

        BasePanel panel = GetPanel(panelType);
        //Debug.Log("push " + panelType);
        panel.transform.position = ScreenPos;
        //Debug.Log("pos " + panel.transform.position);
        panelStack.Push(panel);
        panel.OnEnter();
    }

    public void PopPanel()
    {
        if (panelStack == null)
        {
            panelStack = new Stack<BasePanel>();
        }
        if (panelStack.Count <= 0)
        {
            return;
        }

        //退出栈顶面板
        BasePanel topPanel = panelStack.Pop();
        //Debug.Log("pop " + topPanel);
        topPanel.OnExit();

        //恢复上一个面板
        if (panelStack.Count > 0)
        {
            BasePanel panel = panelStack.Peek();
            panel.OnResume();
        }
    }

    public BasePanel GetPanel(string panelType)
    {

        if (panelDict == null)
        {
            panelDict = new Dictionary<string, BasePanel>();
        }

        BasePanel panel = panelDict.GetValue(panelType);

        //如果没有实例化面板，寻找路径进行实例化，并且存储到已经实例化好的字典面板中
        if (panel == null)
        {
            string path = panelPathDict.GetValue(panelType);
            //Debug.Log(path);
            GameObject panelGo = GameObject.Instantiate(Resources.Load<GameObject>(path), CanvasTransform, false);
            //Debug.Log("panelgo " + panelGo);
            panel = panelGo.GetComponent<BasePanel>();
            panelDict.Add(panelType, panel);
        }
        return panel;
    }

    //解析json文件
    private void ParseUIPanelTypeJson()
    {
        panelPathDict = new Dictionary<string, string>();
        TextAsset textUIPanelType = Resources.Load<TextAsset>("Data/UIPanelTypeJson");
        UIPanelInfoList panelInfoList = JsonMapper.ToObject<UIPanelInfoList>(textUIPanelType.text);

        foreach (UIPanelInfo panelInfo in panelInfoList.panelInfoList)
        {
            panelPathDict.Add(panelInfo.panelType, panelInfo.path);
            //Debug.Log(panelInfo.panelType + ":" + panelInfo.path);
        }
    }

    public void Clear()
    {
        while (panelStack.Count > 0)
            PopPanel();
        //Debug.Log(panelStack.Count);
    }
}
